/*
Copyright (C) 2011  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package GuiTools;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author Christoph Schnack
 *
 */
public class MiniButton extends JButton{
	private TAction action;
	private boolean enabled;
	private Color bgColor = Color.lightGray;
	private Image image,hoveredImage,disabledImage,enabledImage;
	public MiniButton(Image img, Image hlImg, Image dImage, boolean argEnabled, TAction actionArg){
		super();
		enabled = argEnabled;
		enabledImage = img;
		disabledImage = dImage;
		hoveredImage = hlImg;
		image = enabled?enabledImage:disabledImage;
		action = actionArg;
		this.addMouseListener(new MouseListener(){

			public void mouseClicked(MouseEvent arg0) {
				if(enabled)action.perform();
			}
			public void mouseEntered(MouseEvent arg0) {
				image = enabled?hoveredImage:disabledImage;
				repaint();
			}
			public void mouseExited(MouseEvent arg0) {
				image = enabled?enabledImage:disabledImage;
				repaint();
			}
			public void mousePressed(MouseEvent arg0) {
			}
			public void mouseReleased(MouseEvent arg0) {}
			
		});
		this.setPreferredSize(new Dimension(20,20));
	}
	
	public void doClick(){
		if(enabled)action.perform();
	}
	
	public void setEnabled(boolean arg){
		enabled = arg;
		image = enabled?enabledImage:disabledImage;
		repaint();
	}
	
	public void paintComponent(Graphics g){
		g.clearRect(0, 0, 20, 20);
		if(bgColor!=null){
			g.setColor(bgColor);
			g.fillRect(0,0,20,20);
		}
		g.drawImage(image,0,0,this);
	}
}